//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.siecom.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class Util {
    private static final int BUFF_SIZE = 1048576;

    public Util() {
    }

    public static int byte2Int(byte[] byteArray) {
        int integer = 0;
        int shiftCount = 0;

        for(int i = byteArray.length - 1; i >= 0 && i >= byteArray.length - 4; shiftCount += 8) {
            byte b = byteArray[i];
            integer |= (b & 255) << shiftCount;
            --i;
        }

        return integer;
    }

    public static String int2Hex(int i, int length) {
        if(length <= 0) {
            throw new RuntimeException("指定的字符串长度不支持");
        } else {
            long l;
            switch(length) {
                case 1:
                    l = 15L;
                    break;
                case 2:
                    l = 255L;
                    break;
                case 3:
                    l = 4095L;
                    break;
                case 4:
                    l = 65535L;
                    break;
                case 5:
                    l = 1048575L;
                    break;
                case 6:
                    l = 16777215L;
                    break;
                case 7:
                    l = 268435455L;
                    break;
                default:
                    l = 4294967295L;
            }

            String result = Long.toHexString(l & (long)i);
            StringBuilder builder = new StringBuilder(result);

            for(int j = 0; j < length - result.length(); ++j) {
                builder.insert(0, "0");
            }

            return builder.toString().toUpperCase(Locale.getDefault());
        }
    }

    public static String int2String(int interger, int length) {
        String intStr = String.valueOf(interger);
        if(intStr.length() > length) {
            return intStr.substring(intStr.length() - length, intStr.length());
        } else if(intStr.length() == length) {
            return intStr;
        } else {
            StringBuilder builder = new StringBuilder(intStr);

            for(int i = 0; i < length - intStr.length(); ++i) {
                builder.insert(0, '0');
            }

            return builder.toString();
        }
    }

    @SuppressLint({"DefaultLocale"})
    public static String bytesToHexString(byte[] bArray) {
        if(bArray == null) {
            return null;
        } else if(bArray.length == 0) {
            return "";
        } else {
            StringBuffer sb = new StringBuffer(bArray.length);

            for(int i = 0; i < bArray.length; ++i) {
                String sTemp = Integer.toHexString(255 & bArray[i]);
                if(sTemp.length() < 2) {
                    sb.append(0);
                }

                sb.append(sTemp.toUpperCase());
            }

            return sb.toString();
        }
    }

    public static int byte2int2(byte[] b) {
        return b[1] & 255 | (b[0] & 255) << 8;
    }

    public static byte[] concat(byte[] b1, byte[] b2) {
        if(b1 == null) {
            return b2;
        } else if(b2 == null) {
            return b1;
        } else {
            byte[] result = new byte[b1.length + b2.length];
            System.arraycopy(b1, 0, result, 0, b1.length);
            System.arraycopy(b2, 0, result, b1.length, b2.length);
            return result;
        }
    }

    public static int getXB(byte[] bdata2) {
        int j = 0;
        int k = 0;

        for(int i = 0; i < bdata2.length; ++i) {
            if(bdata2[i] == 61 || bdata2[i] == 68) {
                j = i;
                ++k;
                if(k == 1) {
                    return i;
                }
            }
        }

        return j;
    }

    public byte[] byte2Bcd(byte[] abt) {
        if(abt == null) {
            return null;
        } else {
            int bbtLen = abt.length / 2;
            if(bbtLen == 0) {
                return new byte[0];
            } else {
                byte[] bbt = new byte[abt.length / 2];

                for(int p = 0; p < abt.length / 2; ++p) {
                    int j;
                    if(abt[2 * p] >= 48 && abt[2 * p] <= 57) {
                        j = abt[2 * p] - 48;
                    } else if(abt[2 * p] >= 97 && abt[2 * p] <= 122) {
                        j = abt[2 * p] - 97 + 10;
                    } else {
                        j = abt[2 * p] - 65 + 10;
                    }

                    int k;
                    if(abt[2 * p + 1] >= 48 && abt[2 * p + 1] <= 57) {
                        k = abt[2 * p + 1] - 48;
                    } else if(abt[2 * p + 1] >= 97 && abt[2 * p + 1] <= 122) {
                        k = abt[2 * p + 1] - 97 + 10;
                    } else {
                        k = abt[2 * p + 1] - 65 + 10;
                    }

                    int a = (j << 4) + k;
                    byte b = (byte)a;
                    bbt[p] = b;
                }

                return bbt;
            }
        }
    }

    @SuppressLint({"DefaultLocale"})
    public String byteArrayToString(byte[] data) {
        if(data == null) {
            return null;
        } else {
            String str = "";
            String tempStr = "";
            if(data.length == 0) {
                return "";
            } else {
                for(int i = 0; i < data.length; ++i) {
                    tempStr = this.byteToString(data[i]);
                    if(tempStr.length() == 1) {
                        tempStr = "0" + tempStr;
                    }

                    str = str + tempStr;
                }

                return str.toUpperCase();
            }
        }
    }

    @SuppressLint({"DefaultLocale"})
    private String byteToString(byte buf) {
        int n = buf >= 0?buf:256 + buf;
        String str = Integer.toHexString(n);
        return str.toUpperCase();
    }

    @SuppressLint({"DefaultLocale"})
    public char[] getCharByMethod(char[] a, int i, int j) {
        if(a == null) {
            return null;
        } else {
            int N = a.length;
            if(N == 0) {
                return new char[0];
            } else {
                StringBuffer s = new StringBuffer(" ");
                if(i < N && j < N && i <= j) {
                    for(int x = i; x < j; ++x) {
                        s.append(a[x]);
                    }

                    return s.toString().trim().toCharArray();
                } else {
                    return s.toString().toCharArray();
                }
            }
        }
    }

    @SuppressLint({"DefaultLocale"})
    public byte[] StringToBytes(String data) {
        String hexString = data.toUpperCase().trim();
        if(hexString.length() % 2 != 0) {
            return null;
        } else {
            byte[] retData = new byte[hexString.length() / 2];

            for(int i = 0; i < hexString.length(); ++i) {
                char hex_char1 = hexString.charAt(i);
                int int_ch1;
                if(hex_char1 >= 48 && hex_char1 <= 57) {
                    int_ch1 = (hex_char1 - 48) * 16;
                } else {
                    if(hex_char1 < 65 || hex_char1 > 70) {
                        return null;
                    }

                    int_ch1 = (hex_char1 - 55) * 16;
                }

                ++i;
                char hex_char2 = hexString.charAt(i);
                int int_ch2;
                if(hex_char2 >= 48 && hex_char2 <= 57) {
                    int_ch2 = hex_char2 - 48;
                } else {
                    if(hex_char2 < 65 || hex_char2 > 70) {
                        return null;
                    }

                    int_ch2 = hex_char2 - 55;
                }

                int int_ch = int_ch1 + int_ch2;
                retData[i / 2] = (byte)int_ch;
            }

            return retData;
        }
    }

    public static byte getNumHigh(int num) {
        return (byte)((num & '\uff00') >> 8);
    }

    public static byte getNumLow(int num) {
        return (byte)(num & 255);
    }

    public static byte[] hexString2Bytes(String asc) {
        int len = asc.length();
        int mod = len % 2;
        if(mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }

        byte[] abt = new byte[len];
        if(len >= 2) {
            len /= 2;
        }

        byte[] bbt = new byte[len];
        abt = asc.getBytes();

        for(int p = 0; p < asc.length() / 2; ++p) {
            int j;
            if(abt[2 * p] >= 48 && abt[2 * p] <= 57) {
                j = abt[2 * p] - 48;
            } else if(abt[2 * p] >= 97 && abt[2 * p] <= 122) {
                j = abt[2 * p] - 97 + 10;
            } else {
                j = abt[2 * p] - 65 + 10;
            }

            int k;
            if(abt[2 * p + 1] >= 48 && abt[2 * p + 1] <= 57) {
                k = abt[2 * p + 1] - 48;
            } else if(abt[2 * p + 1] >= 97 && abt[2 * p + 1] <= 122) {
                k = abt[2 * p + 1] - 97 + 10;
            } else {
                k = abt[2 * p + 1] - 65 + 10;
            }

            int a = (j << 4) + k;
            byte b = (byte)a;
            bbt[p] = b;
        }

        return bbt;
    }

    public static boolean copyPhotodecodefile(Context context) {
        String filedir = Environment.getExternalStorageDirectory() + "/wltlib";
        File file = new File(filedir);
        if(!file.exists()) {
            if(context == null) {
                return false;
            }

            boolean success = file.mkdir();
            if(success) {
                rwData(context, "wltlib/base.dat", filedir + "/base.dat");
                rwData(context, "wltlib/license.lic", filedir + "/license.lic");
            }
        } else {
            File baseFile = new File(filedir + "/base.dat");
            if(!baseFile.exists()) {
                if(context == null) {
                    return false;
                }

                rwData(context, "wltlib/base.dat", filedir + "/base.dat");
            }

            File licenseFile = new File(filedir + "/license.dat");
            if(!licenseFile.exists()) {
                if(context == null) {
                    return false;
                }

                rwData(context, "wltlib/license.lic", filedir + "/license.lic");
            }
        }

        return true;
    }

    public static void rwData(Context context, String readname, String writename) {
        try {
            InputStream is = context.getAssets().open(readname);
            FileOutputStream os = new FileOutputStream(writename);
            byte[] data = new byte[512];

            while(is.read(data) > 0) {
                os.write(data);
            }

            os.flush();
            os.close();
            is.close();
        } catch (FileNotFoundException var6) {
            var6.printStackTrace();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

    }

    public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
        File desDir = new File(folderPath);
        if(!desDir.exists()) {
            desDir.mkdirs();
        }

        ZipFile zf = new ZipFile(zipFile);
        Enumeration entries = zf.entries();

        while(entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry)entries.nextElement();
            InputStream in = zf.getInputStream(entry);
            String str = folderPath + File.separator + entry.getName();
            str = new String(str.getBytes("8859_1"), "GB2312");
            File desFile = new File(str);
            if(!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if(!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }

                desFile.createNewFile();
            }

            OutputStream out = new FileOutputStream(desFile);
            byte[] buffer = new byte[1048576];

            int realLength;
            while((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }

            in.close();
            out.close();

            try {
                zf.close();
            } catch (Exception var13) {
                var13.printStackTrace();
            }
        }

    }

    public static boolean unGzipFile(String sourceFilePath) {
        Log.e("调试", "开始");
        String outputfilePath = "";

        try {
            File sourceFile = new File(sourceFilePath);
            if(!sourceFile.exists()) {
                Log.e("error", "*.gz not exist");
                return false;
            } else {
                FileInputStream fin = new FileInputStream(sourceFilePath);
                GZIPInputStream gzin = new GZIPInputStream(fin);
                outputfilePath = sourceFilePath.substring(0, sourceFilePath.lastIndexOf(46));
                outputfilePath = outputfilePath + ".bmp";
                File outputfile = new File(outputfilePath);
                if(outputfile.exists() && !outputfile.delete()) {
                    Log.e("warning", "*.bmp delete fail, deletefilePath = " + outputfilePath);
                }

                FileOutputStream fout = new FileOutputStream(outputfile);
                byte[] buf = new byte[1024];

                int num;
                while((num = gzin.read(buf, 0, buf.length)) != -1) {
                    fout.write(buf, 0, num);
                }

                gzin.close();
                fout.close();
                fin.close();
                Log.e("调试", "结束");
                return true;
            }
        } catch (Exception var9) {
            Log.e("调试", "异常");
            var9.printStackTrace();
            return false;
        }
    }

    public static String gzipFile(String sourceFilePath) throws FileNotFoundException, IOException {
        byte[] buf = new byte[2048];
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(sourceFilePath));
        String destFilePath = sourceFilePath + ".gz";

        try {
            BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(destFilePath)));

            int c;
            try {
                while((c = in.read(buf)) != -1) {
                    out.write(buf, 0, c);
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }

        return destFilePath;
    }

    public static String getFileMD5(String filePath) throws IOException, NoSuchAlgorithmException {
        File file = new File(filePath);
        if(!file.isFile()) {
            return null;
        } else {
            MessageDigest digest = null;
            FileInputStream in = null;
            byte[] buffer = new byte[1024];
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);

            int len;
            while((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }

            in.close();
            return byte2HexStr(digest.digest());
        }
    }

    public static boolean deleteFile(String deleteFilePath) {
        try {
            File file = new File(deleteFilePath);
            if(file.exists()) {
                file.delete();
            }

            return true;
        } catch (Exception var2) {
            Log.e("warning", "删除指定文件时出现异常");
            var2.printStackTrace();
            return false;
        }
    }

    public static void copyFile(File sourceFile, File targetFile) throws IOException {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;

        try {
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
            byte[] b = new byte[5120];

            int len;
            while((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }

            outBuff.flush();
        } finally {
            if(inBuff != null) {
                inBuff.close();
            }

            if(outBuff != null) {
                outBuff.close();
            }

        }
    }

    public static byte[] hexStringToBytes(String hexString) {
        if(hexString != null && !hexString.equals("")) {
            hexString = hexString.toUpperCase(Locale.US);
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];

            for(int i = 0; i < length; ++i) {
                int pos = i * 2;
                d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }

            return d;
        } else {
            return null;
        }
    }

    private static byte charToByte(char c) {
        return (byte)"0123456789ABCDEF".indexOf(c);
    }

    public static String str2HexStr(String str) {
        if(str != null && !str.equals("")) {
            byte[] bs = str.getBytes();
            return byte2HexStr(bs);
        } else {
            return "";
        }
    }

    public static String str2HexStr(String str, String charset) {
        if(str != null && !str.equals("")) {
            try {
                byte[] bs = str.getBytes(charset);
                return byte2HexStr(bs);
            } catch (UnsupportedEncodingException var4) {
                throw new RuntimeException("字符串转byte[]出错, 指定的字符集是: " + charset);
            }
        } else {
            return "";
        }
    }

    public static int hexStr2Int(String str) {
        return str != null && !str.equals("")?Integer.parseInt(str, 16):0;
    }

    public static Map<String, String> parseIcUserInfo2(byte[] data, String charsetName) {
        HashMap map = new HashMap();

        try {
            int point = 0;

            while(point < data.length - 4) {
                byte[] tagByte = new byte[1];
                byte[] lenByte = new byte[3];
                tagByte[0] = data[point];
                String tag = new String(tagByte);
                ++point;
                System.arraycopy(data, point, lenByte, 0, 3);
                point += 3;
                int len = Integer.valueOf(new String(lenByte)).intValue();
                byte[] tagDataByte = new byte[len];
                System.arraycopy(data, point, tagDataByte, 0, len);
                point += len;
                String tagData = new String(tagDataByte, charsetName);
                map.put(tag, tagData);
            }
        } catch (Exception var10) {
            var10.printStackTrace();
        }

        return map;
    }

    public static byte[] int2bytes(int value, int size) {
        byte[] buf = new byte[size];

        for(int i = buf.length - 1; i >= 0; --i) {
            int bitCount = 8 * (buf.length - i - 1);
            if(bitCount < 32) {
                buf[i] = (byte)(value >>> bitCount);
            } else {
                buf[i] = 0;
            }
        }

        return buf;
    }

    public static String byte2HexStr(byte[] b) {
        String stmp = "";
        StringBuilder sb = new StringBuilder("");
        if(b != null && b.length > 0) {
            for(int n = 0; n < b.length; ++n) {
                stmp = Integer.toHexString(b[n] & 255);
                sb.append(stmp.length() == 1?"0" + stmp:stmp);
                sb.append("");
            }
        }

        return sb.toString().toUpperCase(Locale.US).trim();
    }

    public static String byte2HexStr(byte[] b, int offset, int length) {
        String stmp = "";
        StringBuilder sb = new StringBuilder("");
        if(b != null && b.length > 0) {
            for(int n = offset; n < length; ++n) {
                stmp = Integer.toHexString(b[n] & 255);
                sb.append(stmp.length() == 1?"0" + stmp:stmp);
                sb.append("");
            }
        }

        return sb.toString().toUpperCase(Locale.US).trim();
    }

    public static byte[] byteXOR(byte[] b) {
        byte[] r = new byte[1];
        if(b != null && b.length > 0) {
            for(int i = 0; i < b.length; ++i) {
                if(i == 0) {
                    r[0] = b[i];
                } else {
                    r[0] ^= b[i];
                }
            }
        }

        return r;
    }

    public static String formatFromCentsToYuan(String numStrInUtilsOfCents) {
        if(numStrInUtilsOfCents != null && !numStrInUtilsOfCents.equals("")) {
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(numStrInUtilsOfCents);
            if(!isNum.matches()) {
                throw new RuntimeException("纯数字字符串：\"" + numStrInUtilsOfCents + "\"，中出现非数字字符");
            } else {
                StringBuilder builder = new StringBuilder(numStrInUtilsOfCents);
                if(builder.length() < 3) {
                    builder.insert(0, "00");
                }

                builder.insert(builder.length() - 2, ".");

                while(builder.charAt(0) == 48 && builder.indexOf(".") != 1) {
                    builder.deleteCharAt(0);
                }

                return builder.toString();
            }
        } else {
            return "";
        }
    }

    public static Bitmap toAlph(Bitmap bmp) {
        return toAlph(bmp, (String)null);
    }

    public static Bitmap toAlph(Bitmap bmp, String outputFilePath) {
        int color = 16777215;
        int bitmap_w = bmp.getWidth();
        int bitmap_h = bmp.getHeight();
        int[] arrayColor = new int[bitmap_w * bitmap_h];
        int count = 0;

        for(int i = 0; i < bitmap_h; ++i) {
            for(int j = 0; j < bitmap_w; ++j) {
                int color1 = bmp.getPixel(j, i);
                if(color1 == -1) {
                    color1 = color;
                }

                arrayColor[count] = color1;
                ++count;
            }
        }

        bmp = Bitmap.createBitmap(arrayColor, bitmap_w, bitmap_h, Config.ARGB_8888);
        if(outputFilePath != null) {
            FileOutputStream out = null;
            File outfile = new File(outputFilePath);
            if(bmp != null) {
                try {
                    out = new FileOutputStream(outfile);
                    bmp.compress(CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();
                } catch (Exception var10) {
                    var10.printStackTrace();
                }
            }
        }

        return bmp;
    }

    public static byte[] getFinalData(byte[] b) {
        byte[] unpackingData = DataParseUtil.unpackingMessage(b);
        if(unpackingData.length == 37) {
            byte[] secondData = new byte[34];
            System.arraycopy(unpackingData, 3, secondData, 0, 34);
            Log.i("secondData", byte2HexStr(secondData));
            String finalDataStr = byte2HexStr(secondData);
            finalDataStr = finalDataStr + "00";
            Log.i("finalDataStr", finalDataStr);
            byte[] finalData = hexString2Bytes(finalDataStr);
            return finalData;
        } else {
            return null;
        }
    }
}
